package com.ky.manage.activity;

import android.util.Log;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import com.google.gson.Gson;
import com.ky.manage.R;
import com.ky.manage.activity.web.OutletsResp;
import com.ky.manage.adapter.OutletsAdapter;
import com.ky.manage.base.BaseActivity;
import com.ky.manage.ui.RecycleViewDivider;
import com.ky.manage.utils.ScreenUtils;
import com.ky.manage.utils.http.AsyncHttpUtils;
import com.ky.manage.utils.http.BusinessHttpReqUtils;
import com.loopj.android.http.TextHttpResponseHandler;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cz.msebera.android.httpclient.Header;

public class OutletsActivity extends BaseActivity {

    RecyclerView recyclerView;
    OutletsAdapter adapter;
    List<OutletsResp.RowsDTO> data = new ArrayList<>();

    private EditText searchEt;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_outlets;
    }

    @Override
    protected void initView() {
        findView(R.id.back_iv, v -> {
            finish();
        });
        recyclerView = findView(R.id.rv_outlets);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL,
                ScreenUtils.dp2px(10), this.getColor(R.color.gray_light)));

        searchEt = findView(R.id.search_et);
        searchEt.setOnEditorActionListener((v, actionId, event) -> {
            switch (actionId) {
                case EditorInfo.IME_ACTION_SEARCH:
                    searchBykeyword(searchEt.getText().toString().trim());
                    break;
                default:
                    break;
            }
            return false;
        });
    }

    @Override
    protected void initData() {
        reqNetPointList();
    }

    private void reqNetPointList() {
        AsyncHttpUtils.getInstance().get(BusinessHttpReqUtils.SERVICE_DOMAIN + "/appApi/getBusinessOutletsList", new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Log.d("lhm", "reqNetPointList onFailure = " + responseString);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Log.d("lhm", "reqNetPointList onSuccess = " + responseString);
                OutletsResp resp = new Gson().fromJson(responseString, OutletsResp.class);
                if(resp != null && resp.getCode() == 200) {
                    if(resp.getRows().size() > 0) {
                        data = resp.getRows();
                        adapter = new OutletsAdapter(OutletsActivity.this, R.layout.item_outlets, data);
                        recyclerView.setAdapter(adapter);
                    }
                }
            }
        });
    }

    /**
     * 关键词搜索
     */
    private void searchBykeyword(String searchStr) {

    }
}
